package com.example.templateapp.util

import android.text.TextUtils
import com.xuexiang.xutil.tip.ToastUtils
import java.util.regex.Pattern

object VerifyUtil {
    private const val ID_CARD =
        "(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$"
    private const val PHONE =
        "^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57]|19[0-9])[0-9]{8}\$"
    private const val NORMAL_CAR =
        "[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}[黑黄蓝绿]{0,1}"
    private const val ENERGY_CAR =
        "[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF])|([DF][A-HJ-NP-Z0-9][0-9]{4}))[黑黄蓝绿]{0,1}"

    fun isIDCard(content: String) = Pattern.matches(ID_CARD, content)


    fun isPhone(content: String) = Pattern.matches(PHONE, content)


    fun isNormalCar(content: String): Boolean = Pattern.matches(NORMAL_CAR, content)


    fun isEnergyCar(content: String) = Pattern.matches(ENERGY_CAR, content)


    fun isCar(content: String) = isNormalCar(content) || isEnergyCar(content)

    /**
     * 如果value为空则toast。‘key不能为空’
     */
    fun isEmptyThenToast(value: String?, key: String): Boolean {
        TextUtils.isEmpty(value).let {
            if (it) ToastUtils.toast("${key}不能为空")
            return it
        }
    }


    fun isPwValid(origin: String, now: String, nowRepeat: String): Boolean {

/*        val originPw: String = MMKV.defaultMMKV().getString(MMKConstant.PW_KEY, "")!!
        if (origin != originPw) {
            ToastUtils.toast("原密码不正确")
            return false
        }*/

        if (now.length < 6 || now.length > 15) {
            ToastUtils.toast("请使密码长度在6-15位")
            return false
        }

        val pattern = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z_]{6,15}$"

        if (!Pattern.matches(pattern, now)) {
            ToastUtils.toast("密码必须由数字和字母组成")
            return false
        }

        if (now != nowRepeat) {
            ToastUtils.toast("两次输入密码不一致")
            return false
        }

        return true
    }
}